package com.gongpingjialoan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AssessHistory;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/5/12.
 */
public class CollectAdapter extends BaseAdapter {

    private List<AssessHistory> mAssessHistories;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private boolean isEdit = false;

    public CollectAdapter(List<AssessHistory> assessHistories, Context context) {
        mAssessHistories = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<AssessHistory> mAssessHistories) {
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }


    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();

    }


    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.collect_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.eval_price);
            holder.reviewTextView = (TextView) convertView.findViewById(R.id.review);
            holder.mCheckBox = (ImageView) convertView.findViewById(R.id.switch_id);
            holder.upTextView = (TextView) convertView.findViewById(R.id.uptext);
            holder.shoujiaTextView = (TextView) convertView.findViewById(R.id.price);
            holder.eval_View = convertView.findViewById(R.id.eval_layout);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        AssessHistory source = mAssessHistories.get(position);
        convertView.setTag(R.id.tag_second, source);

        if (isEdit) {
            holder.mCheckBox.setVisibility(View.VISIBLE);
        } else {
            holder.mCheckBox.setVisibility(View.GONE);
            source.isDelete = false;
        }

        if (Integer.parseInt(source.price_decrease) < 0) {
            holder.upTextView.setText("涨价了");
            holder.upTextView.setTextColor(Color.parseColor("#FB787A"));
        } else if (Integer.parseInt(source.price_decrease) > 0) {
            holder.upTextView.setText("降价了");
            holder.upTextView.setTextColor(Color.parseColor("#31C27C"));
        } else {
            holder.upTextView.setVisibility(View.GONE);
        }

        if (source.isDelete) {
            holder.mCheckBox.setImageResource(R.drawable.checked);
        } else {
            holder.mCheckBox.setImageResource(R.drawable.uncheck);
        }

        if ("false".equals(source.review)) {
            holder.reviewTextView.setVisibility(View.GONE);
        } else {
            holder.reviewTextView.setText("已下架");
            holder.reviewTextView.setVisibility(View.VISIBLE);
        }
        holder.shoujiaTextView.setText("车主售价: " + source.price + "万");
//        holder.domainTextView.setText(source.d);
        holder.tvCarTitle.setText(source.model_detail);
        holder.mileTextView.setText(source.year + "年上牌/" + source.mile + "万公里");


        if ("promo_car".equals(source.record_type)) {
            holder.priceTextView.setText(source.promo_price);
        } else {
            if("".equals(source.eval_price) || Utils.isLing(source.eval_price)){
                holder.eval_View.setVisibility(View.GONE);
            }else{
                holder.eval_View.setVisibility(View.VISIBLE);
            }
            holder.priceTextView.setText(source.eval_price);
        }
        Glide.with(mContext).load(source.url).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        return convertView;
    }

    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView shoujiaTextView;
        public TextView reviewTextView;

        public TextView upTextView;

        public ImageView mCheckBox;

        public  View eval_View;

    }
}